//
//  MZExtensionDisptchQueue.swift
//  MZTool
//
//  Created by gby on 2022/8/23.
//

import Foundation

//MARK:时间戳
extension DispatchTime: ExpressibleByIntegerLiteral {
    public init(integerLiteral value: Int) {
        self = DispatchTime.now() + .seconds(value)
    }
}
extension DispatchTime: ExpressibleByFloatLiteral {
    public init(floatLiteral value: Double) {
        self = DispatchTime.now() + .milliseconds(Int(value * 1000))
    }
}

extension DispatchQueue{
    private static var _onceTracker = [String]()
    public class func once(token: String, block: () -> ()) {
        objc_sync_enter(self)
        defer {
            objc_sync_exit(self)
        }
        if _onceTracker.contains(token) {
            return
        }
        _onceTracker.append(token)
        block()
    }
}

extension MZKit where Base == DispatchQueue {
    
    public func after(_ delay: TimeInterval, execute: @escaping () -> Void) {
        base.asyncAfter(deadline: .now() + delay, execute: execute)
    }
    
    public func after(time: DispatchTime, block: @escaping ()->()) {
        base.asyncAfter(deadline: time, execute: block)
    }
    
    public func async(block: @escaping ()->()) {
        base.async(execute: block)
    }
}


